<!-- Ajax search -->

<script src="Ajax/Ajax_Search.js"></script>

<!-- Enf of Ajax Search -->

<form method="post">
	<p><input name="search_name" OnKeyUp="showHint(this.value)" style="width:100%";></p>
	<p><input type="submit" value="Search"></p>
	<input type="checkbox" name="cat1" value="1"><i>Xvid</i>
	<input type="checkbox" name="cat2" value="1"><i>Iso</i>
	<input type="checkbox" name="cat3" value="1"><i>Other</i>

	<?echo "<p>Suggestions: <span id=\"txtHint\"></span></p> ";?>
</form>

<?

//Letar efter dumma fel av dumma personer
if(!empty($_POST["search_name"])){
	?>
	
	<table>
	<tr>
	<td>Cat</td>
	<td>Name</td>
	<td>Size</td>
	<td>Seeders</td>
	<td>Leechers</td>
	</tr>
	<?
	for($i = 1; $i <= 2; $i++){
		if(!empty($_POST["cat".$i])){
			if($_POST["cat".$i] != 1 ){
				die("Why is cat".$i." = ".$_POST["cat".$i]."?");
			}
		}
	}

	$name = mysql_escape_string($_POST["search_name"]);
	$torrents_name = mysql_query("SELECT * FROM `torrents` WHERE `name` like '%".$name."%' ORDER BY `name`;") or die(mysql_error());

	while($data = mysql_fetch_array( $torrents_name )){
				
		$torrent_data = bdec($data['data']);
		$c = count($torrent_data['info']['files']);
		$size = 0; 
		if ($c > 1) {
			for ($i = 0; $i < $c; $i++) {
				$size += round((($torrent_data['info']['files'][$i]['length'])/1048576),2);
			}
		} else {
			echo $torrent_data['info']['name'] . '<br>';
		}
		
		$size = size($size);
	
		echo "<tr>\n";
		echo "<td>".$data['cat']."</td>\n";
		echo "<td><a href=\"TorrentInfo&name=".$data['name']."\">".$data['name']."</a></td>\n";
		echo "<td>".$size."</td>\n";
		echo "</tr>\n";
		
		
		
	}
}
?>
</table>